package com.fuint.minio.minIOConst;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName : bucketConst
 * @Description : minIO 常量
 * @Author : wzq
 * @Date: 2022-01-11 10:18
 */
public class BucketConst {

    /**
     * 数据桶
     */
    public static final String FUNINT_BUCKET = "fuint";

    /**
     * 桶 img永久访问策略
     */
    public static final String IMG_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::istar-data\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\",\"s3:AbortMultipartUpload\",\"s3:DeleteObject\"],\"Resource\":[\"arn:aws:s3:::istar-data/*\"]}]}";
    public static final String DOWN_POLICY = "{\"Statement\":[{\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::wangtest\"},{\"Action\":\"s3:GetObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::wangtest/*\"}],\"Version\":\"2012-10-17\"}";
    /**
     * JSON文件后缀
     */
    public static final String JSON_FILE_SUFFIX = ".json";

    /**
     * 获取该桶的策略
     */
    public static String getPolicy(String bucketName){
        String policyJson = "{\n" +
                "\t\"Statement\": [{\n" +
                "\t\t\"Action\": \"s3:GetObject\",\n" +
                "\t\t\"Effect\": \"Allow\",\n" +
                "\t\t\"Principal\": \"*\",\n" +
                "\t\t\"Resource\": \"arn:aws:s3:::fuint/*\"\n" +
                "\t}],\n" +
                "\t\"Version\": \"2012-10-17\"\n" +
                "}";

        JSONObject object = JSON.parseObject(policyJson);
        JSONArray array = object.getJSONArray("Statement");
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            jsonObject.put("Resource","arn:aws:s3:::"+bucketName+"/*");
        }
        return object.toJSONString();
    }
}
